//
// Created by 吴喆 on 2020/3/4.
//

#include "Texture.h"

Texture2D::Texture2D()
        : width(0), height(0), internalFormat(GL_RGB),
          imageFormat(GL_RGB), wrapS(GL_REPEAT), wrapT(GL_CLAMP_TO_EDGE),
          filterMin(GL_NEAREST_MIPMAP_LINEAR), filterMax(GL_NEAREST), id(0) {
    glGenTextures(1, &this->id);
}

void Texture2D::generate(GLuint i, GLuint j, unsigned char *data) {
    this->width = i;
    this->height = j;
    // Create Texture
    glBindTexture(GL_TEXTURE_2D, this->id);
    glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, i, j, 0, this->imageFormat, GL_UNSIGNED_BYTE, data);
    // Set Texture wrap and filter modes
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->wrapS);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->wrapT);
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, this->filterMin);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, this->filterMax);
    glGenerateMipmap(GL_TEXTURE_2D);
    // Unbind texture
    glBindTexture(GL_TEXTURE_2D, 0);

}

void Texture2D::bind() const {
    glBindTexture(GL_TEXTURE_2D, this->id);
}
